package com.example.time.pojo;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

/**
 * @author linfeng
 * @date 2022/1/17 16:23
 */
public class UnixTime {

    private long timestamp;

    public UnixTime(long timestamp) {
        if (timestamp < 0) {
            throw new IllegalArgumentException("timestamp不应为负数");
        }
        this.timestamp = timestamp;
    }

    @Override
    public String toString() {
        Date date = new Date(timestamp);
        Instant instant = date.toInstant();
        ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
        return zonedDateTime.toString();
    }

    public static void main(String[] args) {
        UnixTime unixTime = new UnixTime(System.currentTimeMillis());
        System.out.println(unixTime);
    }
}
